﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.OA
{
    public partial class _review_equal : PageBase
    {
        public string whe = "", stype = "";
        public string us, tab = "oa_form_eqau";
        protected void Page_Load(object sender, EventArgs e)
        {  
            us=Session["ID"].ToString();
            if (!IsPostBack)
            {
                btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
                fill();
                BindGrid();
            }
        }

        private void fill()
        {
            DataTable d = BLL.BLLfun.get_search(tab);
            ddlfiled.Items.Clear();
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            }
            ddlfiled.DataBind();

          


            DataTable e = BLL.BLLfun.get_where();
            ddlwhere.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            }
             ddlwhere.DataBind();   ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));


            DataTable du = BLL.oa.leave_sub.getlist_eqau("");
            ddlform.Items.Clear();
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlform.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["formname"].ToString(), du.Rows[i]["formname"].ToString()));
            }
            ddlform.DataBind();
         

            DataTable du2 = BLL.oa.review.get_list_group("");          
            ddlprocess.Items.Clear();
            for (int i = 0; i < du2.Rows.Count; i++)
            {
                ddlprocess.Items.Add(new ExtAspNet.ListItem(du2.Rows[i]["retitle"].ToString(), du2.Rows[i]["retitle"].ToString()));

            }
            ddlprocess.DataBind();
            ddlprocess.Items.Add(new ExtAspNet.ListItem("", ""));

        }

        public  string getnm(string nmid)
        {
            BLL.sys.sys_user u = new BLL.sys.sys_user();
            return u.getname(nmid);
        }


        private void BindGrid()
        {
            DataTable  table2;

            table2 = BLL.oa.leave_sub.getlist_eqau(whe);
            Grid2.DataSource = table2;
            Grid2.DataBind();          
        }      

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }

        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = BLL.oa.leave_sub.getlist_eqau(whe);
            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
           
            //DataTable table = BLL.oa.ad.get_list(stype);
            DataTable ds = BLL.oa.review.get_list("retitle='" + ddlprocess.SelectedValue + "'");
            DataView view1 = ds.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, "ASC");

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }  


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = txt.Text;
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
                if (ddlf == "sid" || ddlf == "编号")
                    whe = "" + ddlf + " " + ddlw + " " + key + "";
                else
                    whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();

            foreach (int row in Grid2.SelectedRowIndexArray)
            {
                sb.Append(Grid2.DataKeys[row+val][0].ToString());
                sb.Append(",");
            }

            //Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));

           
            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            BindGrid();

        }


        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "edit") //修改 
            {
                string DD = Grid2.DataKeys[e.RowIndex][0].ToString();
                DataTable dt1 = BLL.oa.leave_sub.getlist_eqau("sid="+DD+"");           
                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();

                     ddlform.SelectedIndex =
                     ddlform.Items.IndexOf(ddlform.Items.FindByValue(dt1.Rows[0]["formname"].ToString().Trim()));

                ddlprocess.SelectedIndex =
                     ddlprocess.Items.IndexOf(ddlprocess.Items.FindByValue(dt1.Rows[0]["forproces"].ToString().Trim()));
            }
            else if (e.CommandName == "Del")//删除 
            {

            } 

        }

 
        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            string key = txt.Text;
          //  stype = hidstype.Text;
            try
            {
                if (key == "") whe = BLL.pub.pubwhere.get_where_sql(us, tab);
                BindGrid();

            }
            catch (Exception ex)
            {
                Alert.Show("查询条件错误");
            }
            finally
            {
                whe = "";
            }
        }

        protected void btsave_Click(object sender, EventArgs e)
        {
            string user=us;

            string sid = ddlform.SelectedValue;
           
            string dus = ddlprocess.SelectedValue;
         
          
              BLL.oa.leave_sub.uplist_eqau(sid,dus);     

            hidsid.Text = "";
            BindGrid();
        }

        protected void ddlprocess_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable ds = BLL.oa.review.get_list("retitle='" + ddlprocess.SelectedValue+ "'");
            Grid1.DataSource = ds;
            Grid1.DataBind();
        }


    
    }
}
